package elion.com.theElion.fragmentLandData;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import elion.com.theElion.chartline.LineActivity;
import elion.com.elion.R;
import elion.com.theElion.util.Info;
import elion.com.theElion.util.ResUtil;
import elion.vo.elion_Data.Elion_water;

import static elion.com.theElion.util.HttpUtils.sendPostMethod;
import static elion.com.theElion.util.ResUtil.changeToFieldName;

/**
 * Description：WaterDataFragment
 * Created by：CaMnter
 * Time：2015-10-17 12:15
 */
public class WaterDataFragment extends Fragment {

    private volatile View mView;
    private String landId;
    private String landName;

    private ListView mDataListView;
    private SimpleAdapter mListAdapter;
    private List<Map<String, Object>> dataList;//

    private Elion_water elion_water;


    @SuppressLint("InflateParams")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_data_show, null);
//三个Fragment公用一个布局
        Intent intent =getActivity().getIntent();
        landId = intent.getStringExtra("landId");
        landName = intent.getStringExtra("landName");

        findView();
        init();
        return mView;
    }
    private void findView()

    {
        mDataListView = (ListView) mView.findViewById(R.id.data_listview);
        mDataListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
            {

                Integer num = 24;
                String lineUrl = Info.path_SelectWater(landId,num.toString());
                String beanClass = elion_water.getClass().getName();
                String fieldName= changeToFieldName((String) dataList.get(position).get("dataName"));
                Intent intent = new Intent(getActivity(),LineActivity.class);

                Bundle bundle = new Bundle();
                bundle.putString("TYPE",beanClass);
                bundle.putString("FIELD_NAME",fieldName);
                bundle.putInt("NUMBER",num);
                bundle.putString("PATH",lineUrl);
                bundle.putString("LANDID",landId);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });


    }
    private void init()
    {
        new queryDataAsyncTask().execute(Info.path_SelectWater(landId,"1"));
    }

    class queryDataAsyncTask extends AsyncTask<String, Void, String>
    {

        @Override
        protected String doInBackground(String... strings)
        {
            System.out.println(strings[0]+"异步加载");
            return sendPostMethod(strings[0], "utf-8");
        }
        @Override
        protected void onPostExecute(String result)
        {

            super.onPostExecute(result);
            if (result==null||result.equals(""))
            {
                Log.w("水位","进入result查询失败分支");
                elion_water=new Elion_water("失败","失败","失败");
            }
            else
            {
                Log.i("土壤数据","进入result不为空分支");
                //JSON解析

            }
            try
            {
                try
        {
            elion_water = (Elion_water) ResUtil.jsonMyData(result,Elion_water.class);
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        } catch (InvocationTargetException e)
        {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e)
        {
            e.printStackTrace();
        }
        } catch (NoSuchMethodException e)
            {
                e.printStackTrace();
            }

            try
            {
                dataList = ResUtil.fromBeanToList(elion_water);
                //将json解析到的bean转化为适配器可用的dataList
            } catch (NoSuchMethodException e)
            {
                e.printStackTrace();
            } catch (IllegalAccessException e)
            {
                e.printStackTrace();
            } catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }
            mListAdapter = new SimpleAdapter(getActivity(), dataList,
                    R.layout.fragement_data_show_item, new String[]{"dataName",
                    "dataValue", "dataUnit"}, new int[]{
                    R.id.tv_data_name,
                    R.id.tv_data_vaule, R.id.tv_data_unit});
//            R.id.iv_data_item,和dataImg为图片，在ResUtil中的bean变数组函数，也没有将img加上。

            mDataListView.setAdapter(mListAdapter);
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }
    }
}
